package main

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gen"
	"gorm.io/gorm"
)

const MySqlDsn = "root:123456@tcp(127.0.0.1:3307)/db_lion_001?charset=utf8mb4&parseTime=True&loc=Local"

func ConnectDB(dsn string) (*gorm.DB) {
	gormdb, err := gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(fmt.Errorf("connect db fail: %w", err))
	}
	return gormdb
}

func main() {
	g := gen.NewGenerator(gen.Config{
		OutPath: "./internal/dao/query",
		Mode:    gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
	})

	g.UseDB(ConnectDB(MySqlDsn)) // reuse your gorm db
	g.ApplyBasic(g.GenerateAllTable()...)
	g.Execute()
}

type Querier interface {
	// SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}}
	FilterWithNameAndRole(name, role string) ([]gen.T, error)
}